package cn.edu.zafu.bigdata.x.hjn;

import java.io.*;
import java.net.*;

public class TCPServer {
    public static void main(String[] args) {
        try {
            // 创建服务器端的服务端口，监听客户端连接
            ServerSocket serverSocket = new ServerSocket(12345);
            System.out.println("服务器已启动，等待客户端连接...");

            // 等待客户端连接
            Socket socket = serverSocket.accept();
            System.out.println("客户端已连接：" + socket.getInetAddress());

            // 获取输入输出流
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            String clientMessage;
            while ((clientMessage = in.readLine()) != null) {
                System.out.println("收到客户端消息: " + clientMessage);
                out.println("服务器回复: " + clientMessage);  // 发送回复到客户端
            }

            // 关闭资源
            in.close();
            out.close();
            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
